import { _decorator, Color, Component, Node, Sprite, Vec2, Vec3 } from 'cc';
import { SpriteUtil } from '../Utils/SpriteUtil';
import { ChessCommon } from './ChessCommon';
import { ChessUIPanel } from './ChessUIPanel';
const { ccclass, property } = _decorator;

@ccclass('ChessMan')
export class ChessMan extends Component {
    /**
     * 棋盘列
     */
    private _x:number = 0;
    public get x():number{
        return this._x;
    }
    public set x(value:number){
        this._x = value;
    }

    /**
     * 棋盘行
     */
    private _y:number = 0;
    public get y():number{
        return this._y;
    }
    public set y(value:number){
        this._y = value;
    }

    public updateXY(x:number, y:number):void{
        this._x = x;
        this._y = y;
        if(this._key == "M0"){
            console.log("updateXY: " + x + "," + y);
        }
    }
    /**
     * 棋子:
     */
    private _pater:string = "";
    public get pater():string{
        return this._pater;
    }
    /**
     * 
     */
    private _key:string;
    public get key():string{
        return this._key;
    }
    /**
     * 阵营 1:红方，-1:蓝方
     */
    private _my:number;
    /**
     * 阵营 1:红方，-1:蓝方
     */
    public get my() : number {
        return this._my;
    }

    /**
     * isShow:
     */
    private _isShow:boolean = false;
    public get isShow() : boolean {
        return this._isShow;
    }
    
    /**
     * 中文名
     */
    private _text:string;
    public get text() : string {
        return this._text;
    }
    /**
     * 权重
     */
    private _value:number[][];
    public get value() : number[][] {
        return this._value;
    }
    /**
     * 获取所有落子点
     */
    private _ps:number[][];
    public get ps():number[][]{
        return this._ps;
    }

    /**
     * 图片
     */
    private _sprite:Sprite;
    private get sprite():Sprite{
        if(null == this._sprite){
            this._sprite = this.node.getComponent(Sprite);
        }
        return this._sprite;
    }

    /**
     * 透明度 (选中棋子半透明显示)
     */
    private _alpha:number = 1.0;
    public set alpha(value:number){
        this._alpha = value;
        this.sprite.color = new Color(255, 255, 255, value * 255);
    }

    start() {
        
    }

    // update(deltaTime: number) {}
    /**
     * 棋子
     * @param value 
     * @returns 
     */
    public SetKey(value:string):ChessMan{
        this._key = value;
        this._pater = value.slice(0, 1);
        var o = ChessCommon.args[this._pater];
        this._my = o.my;
        this._text = o.text;
        this._value = o.value;
        this.name = this._text;
        //
        SpriteUtil.Instance.setChessAtlasSpriteFrame(this.sprite, o.img, null);
        return this;
    }

    /**
     * 
     * @param x 
     * @param y 
     * @param key 
     * @returns 
     */
    public SetXY(x:number, y:number):ChessMan{
        this._x = x;
        this._y = y;
        this.SetPos(ChessCommon.convertRowColToChessPos(this.x, this.y));
        return this;
    }

    /**
     * 
     * @param pos 
     * @returns 
     */
    public SetPos(pos:Vec3):ChessMan{
        this.node.setPosition(pos);
        return this;
    }

    /**
     * 
     * @param value 
     * @returns 
     */
    public SetShow(value:boolean):ChessMan{
        this._isShow = value;
        this.node.active = value;
        this.alpha = value ? 1.0 : 0.0;
        return this;
    }
    /**
     * 获取所有落子点
     */
    public blps(map:string[][]):number[][]{
        var o = ChessCommon.args[this._pater];
        this._ps = ChessCommon.bylaw[o.bl](this.x, this.y, map, this.my);

        return this._ps;
    }
}

